package com.xinghou.platform.dao.wzk.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.ExposureImageDAO;
import com.xinghou.platform.dao.main.WzkExposureImageDAO;
import com.xinghou.platform.model.main.ExposureImage;
import com.xinghou.platform.util.ConfigUtil;

/**
 * 
 * @author xuzhen
 *
 */
@Service
public class WzkExposureImageDAOImpl implements WzkExposureImageDAO {
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public ExposureImage getExposureImageByid(Integer id) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT * FROM exposure_image where Id =? and status>0";
		ExposureImage exposureImage = jdbcTemplate.queryForObject(sql, new Object[] {id}, new ExposureImageMapper());
		CustomerContextHolder.clearCustomerType();
		return exposureImage;
	}

	@Override
	public int updateExposureImage(ExposureImage exposureImage)
			throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"UPDATE wzk.exposure_image SET User_id =?,Exp_id=?,Uri =?,STATUS=?,Praise_count=?,Create_time=?,image_type=? Where Id =?"	
				);
		Object[] params = {exposureImage.getUserId(),exposureImage.getExpId(),exposureImage.getUri(),exposureImage.getStatus(),exposureImage.getPraiseCount(), exposureImage.getCreateTime(),exposureImage.getImageType(),exposureImage.getId()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	@Override
	public int saveExposureImage(ExposureImage exposureImage)
			throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"INSERT INTO exposure_image (User_id,Exp_id,Uri,STATUS,Praise_count,Create_time,image_type)VALUES(?,?,?,?,?,?,?)"
				);
		Object[] params = {exposureImage.getUserId(),exposureImage.getExpId(),exposureImage.getUri(),exposureImage.getStatus(),exposureImage.getPraiseCount(),exposureImage.getCreateTime(),exposureImage.getImageType()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret =jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	@Override
	public List<ExposureImage> getExposureImagelistByExpId(int expId) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql="SELECT * FROM wzk.exposure_image where Exp_id =? and status>0";
		List<ExposureImage> list = jdbcTemplate.query(sql, new Object[] {expId}, new ExposureImageMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}

	@Override
	public int deleteExposureImage(ExposureImage exposureImage)
			throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"DELETE FROM exposure_image WHERE Id = ? AND Exp_id = ?"	
				);
		Object[] params = {exposureImage.getId(),exposureImage.getExpId()};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}
	
	private static final class ExposureImageMapper implements RowMapper<ExposureImage> {
		public ExposureImage mapRow(ResultSet rs, int rowNum) throws SQLException {
			ExposureImage exposureImage = new ExposureImage();
			exposureImage.setExpId(rs.getInt("Exp_id"));
			exposureImage.setCreateTime(rs.getTimestamp("Create_time"));
			exposureImage.setId(rs.getInt("Id"));
			exposureImage.setPraiseCount(rs.getInt("Praise_count"));
			exposureImage.setStatus(rs.getShort("STATUS"));
			exposureImage.setUri(rs.getString("Uri"));
			exposureImage.setUserId(rs.getInt("User_id"));
			exposureImage.setImageType(rs.getShort("image_type"));
			return exposureImage;
		}
	}
}
